package com.oe.market.modules.ele.model.dao;

import com.oe.market.modules.ele.model.bean.Cookie;
import com.oe.market.modules.common.model.ADaoBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.List;

public class CookieDao extends ADaoBase<Cookie> {

    private static final Logger log = LoggerFactory.getLogger(CookieDao.class);
    public final static CookieDao me = new CookieDao();


    public List<Cookie> findByAccountId(Integer accountId){
        String sql = "SELECT oeasy_ele_cookie_info.create_time, oeasy_ele_cookie_info.cookie, oeasy_ele_cookie_info.body, oeasy_ele_cookie_info.sns_hash FROM oeasy_ele_cookie_use_count , oeasy_ele_cookie_info WHERE oeasy_ele_cookie_use_count.from_account_id = "+accountId
                +" AND oeasy_ele_cookie_use_count.sns_hash = oeasy_ele_cookie_info.sns_hash";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<Cookie> findByCookieId(Integer cookieId) {
        String sql = "select * from " + getTableName() + " where cookie_id=" + cookieId + ";";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<Cookie> findBySnsHash(String snsHash) {
        String sql = "select * from " + getTableName() + " where sns_hash='" + snsHash + "';";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<Cookie> findLastBySnsHash(String snsHash) {
        String sql = "select * from " + getTableName() + " where sns_hash='" + snsHash
                + "' order by `create_time` desc limit 1;";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    public List<Cookie> searchUse() {
        String sql = "SELECT i.cookie, i.body, i.sns_hash, i.cookie_id  FROM oeasy_ele_cookie_use_count AS c  INNER JOIN oeasy_ele_cookie_info AS i WHERE c.info_id = i.cookie_id  AND  c.max_use_day > c.today_use AND status != 3 ;";
        try {
            return find(sql);
        } catch (SQLException e) {
            log.error(e.getMessage());
            return null;
        }
    }

    @Override
    public Cookie getThisClassInstance() {
        return new Cookie();
    }

    @Override
    public String getTableName() {
        return "oeasy_ele_cookie_info";
    }


}
